const app = getApp();
const publicFun = require('/utils/public');
import { getMedalList, getMedal } from '/utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperHeight: 0,
    current: 0,
    indicatorDots: true,
    circularType: true,
    medalList: [],
    getMask: false,
    medalData: null,
    typeid: 9,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
    let systemInfo = app.globalData.systemInfo;
    let wdH = systemInfo.windowHeight;
    this.setData({
      systemInfo: systemInfo,
      swiperHeight: wdH-(systemInfo.titleBarHeight+systemInfo.statusBarHeight)
    });
    this.getMedalList(); 
  },

  medalMaskOpen(e) {
    let data = e.currentTarget.dataset;
    let listAll = this.data.medalList;
    if(data.item.IsCanUp == 2){ 
      for(let i=0;i<listAll.length;i++){
        if(listAll[i].IsCanUp == 2){
          if(this.data.current == i){
            this.getMedal(data.item.Id); // 获取勋章
          }else{
            this.setData({
              current: i,
            });
            return false;
          }
        }
      }
      
    }else{
      my.navigateTo({
        url:'/pages/cardList/cardList'
      })
    }
  },
  closeTypeMask() {
    if(this.data.getMask == true){
      this.data.medalList = [];
      this.getMedalList();
    }
    this.setData({
      getMask: false,
    })
  },
  jump(e) {
    let that = this;
    let data = e.currentTarget.dataset;
    my.switchTab({
      url: '/pages/index/index'
    })
  },

  async getMedal(xunzhangId) {
    let that = this;
    try {
      let params = {
        userid: app.globalData.userid ? app.globalData.userid : 0,
        typeid: this.data.typeid,
        xunzhangid: xunzhangId
      }
      my.showLoading();
      const res = await getMedal(params)
      my.hideLoading();
      if (res.S == 1) {
        publicFun.showToast(res.M);
        let resData = {
          Img: res.Img,
          OldJB: res.OldJB,
          NewJB: res.NewJB,
          Info: res.Info
        }
        that.setData({
          medalData: resData,
          getMask: true, // 领取成功弹窗显示
        });
      }else{
        publicFun.showToast(res.M);
      }
    } catch (e) {
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },
  async getMedalList() {
    let that = this;
    my.showLoading();
    try {
      let params = {
        userid:  app.globalData.userid ? app.globalData.userid : 0,
        typeid: this.data.typeid,
      }
      const res = await getMedalList(params)
      my.hideLoading();
      if (res.S == 1) {
        var medalList = [];
        if(res.L.length > 0){
          let thatIndex = 0;
          res.L.forEach((item, index) => {   
            if(Number(item.HaveCount) > Number(item.Line)){
              if(item.IsCanUp == 1){
                thatIndex = index == 0 ? 0 : index - 1
              }else{
                thatIndex = index
              }
            };
           medalList.push(item);
          });

          that.setData({
            medalList: medalList,
            current: thatIndex
          })
        }
      }else{
        publicFun.showToast(res.M);
      }
    } catch (e) {
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },
})